package com.example.homeworkmutualevaluation.Common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestControllerAdvice
public class ExceptionHandlerAdvice {

    @ExceptionHandler(DataAccessException.class)
    public ResponseEntity<Map<String, Object>> handleDatabaseExceptions(DataAccessException ex) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 0);
        response.put("msg", "SQL错误");
        log.error(ex.toString());
        return new ResponseEntity<>(response, HttpStatus.OK);
    }
}
